import { CoolController, BaseController } from '@cool-midway/core';
import { Body, Get, Inject, Post, Query } from '@midwayjs/core';
import { TaskJobService } from '../../service/job';

/**
 * 任务
 */
@CoolController()
export class AppTaskJobController extends BaseController {
  @Inject()
  taskJobService: TaskJobService;

  @Inject()
  ctx;

  @Post('/save', { summary: '创建|更新' })
  async save(@Body() data) {
    return this.ok(await this.taskJobService.save(data, this.ctx.appInfo.id));
  }

  @Get('/detail', { summary: '详情' })
  async detail(@Query('jobId') jobId: string, @Query('topic') topic: string) {
    return this.ok(
      await this.taskJobService.detail(jobId, topic, this.ctx.appInfo.id)
    );
  }

  @Post('/remove', { summary: '移除' })
  async remove(@Body('jobId') jobId: string, @Body('topic') topic: string) {
    await this.taskJobService.remove(jobId, topic, this.ctx.appInfo.id);
    return this.ok();
  }

  @Post('/stop', { summary: '停止' })
  async stop(@Body('jobId') jobId: string, @Body('topic') topic: string) {
    await this.taskJobService.stop(jobId, topic, this.ctx.appInfo.id);
    return this.ok();
  }

  @Post('/start', { summary: '启动' })
  async start(@Body('jobId') jobId: string, @Body('topic') topic: string) {
    await this.taskJobService.start(jobId, topic, this.ctx.appInfo.id);
    return this.ok();
  }

  @Post('/result', { summary: '结果' })
  async result(
    @Body('recordId') recordId: number,
    @Body('status') status: number,
    @Body('result') result: string
  ) {
    await this.taskJobService.result(recordId, status, result);
    return this.ok();
  }
}
